from rest_framework.viewsets import GenericViewSet
from rest_framework.views import APIView
from django.core.exceptions import ObjectDoesNotExist
from rest_framework import mixins
from rest_framework.response import Response
from user.models import EmployeeConfig
from user.serializers import EmployeeSerializer
from django.utils.decorators import method_decorator
from libs.utils.decorator import request_log
from xmanage.response import OBJECT_NOT_FOUND, API_SUCCESS

# Create your views here.
# mixins.RetrieveModelMixin, mixins.UpdateModelMixin,GenericViewSet
class EmployeeConfigView(GenericViewSet):
     queryset = EmployeeConfig.objects.all()
     serializer_class = EmployeeSerializer

     @method_decorator(request_log(level='INFO'))
     def update(self, request, **kwargs):
         pk = 1
         print("aaaaa")

         api_body = {
             'title': request.POST['title'],
             'imageurl': request.POST['imageurl']
         }

         print(api_body)

         try:
             EmployeeConfig.objects.filter(id=pk).update(**api_body)
         except ObjectDoesNotExist:
             return Response(OBJECT_NOT_FOUND)

         return Response(API_SUCCESS)

     @method_decorator(request_log(level='INFO'))
     def get(self, request, **kwargs):
         """
         查询员工配置
         """
         try:
             config = EmployeeConfig.objects.get(id=1)
             print("heelo")
         except ObjectDoesNotExist:
             return Response(OBJECT_NOT_FOUND)

         return Response(EmployeeSerializer(config).data)



